package com.chen.controller;

import com.chen.pojo.Building;
import com.chen.pojo.Content;
import com.chen.pojo.Notice;
import com.chen.pojo.Teacher;
import com.chen.service.NoticeService;
import com.chen.utils.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @RequestMapping("to_addNotice.html")
    public String toAddNotice(){
        return "/WEB-INF/pages/admin/addNotice";
    }
    @RequestMapping("addNotice.html")
    @ResponseBody
    public String addNotice(String title,String content){

        try {
            Notice notice = new Notice();
            notice.setTitle(title);
            Date date = new Date();
            notice.setCreateDate(date);
            noticeService.addNotice(notice);
            //根据标题查出来公告；
            Notice notice1=noticeService.getNoticeByTitle(title);
            Content content1 = new Content();
            content1.setNid(notice1.getId());
            content1.setContent(content);
            noticeService.addContent(content1);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "defeade";
        }
    }
    @RequestMapping("to_showNoticeList.html")
    public String toShowNotice(HttpServletRequest request){
        Integer role = (Integer)request.getSession().getAttribute("USER_TYPE");

       if(role==1){
           return "/WEB-INF/pages/admin/NoticeList";
       } if(role==2){
            return "/WEB-INF/pages/teacher/NoticeList";
        }else{
            return "/WEB-INF/pages/student/NoticeList";
        }
    }
    @RequestMapping("getNoticeList.html")
    @ResponseBody
    public PageInfo<Notice> getNoticeList(Notice notice, @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,HttpServletRequest request){

        PageHelper.startPage(pageNum, Constants.PAGESIZE);
        List<Notice> notices = noticeService.getNoticeList(notice);
        PageInfo<Notice> pageInfo = new PageInfo<Notice>(notices);

        return pageInfo;
    }

    //修改公告
    @RequestMapping("to_updateNotice.html")
    public String toUpdateNotice(Integer id,HttpServletRequest request){
        Notice notice=noticeService.getNoticeById(id);
        request.setAttribute("notice",notice);
        Integer role = (Integer)request.getSession().getAttribute("USER_TYPE");
       if(role==1){
           return "/WEB-INF/pages/admin/updateNotice";
       } if(role==2){
            return "/WEB-INF/pages/teacher/updateNotice";
        }else{
            return "/WEB-INF/pages/student/updateNotice";
        }
    }
    @RequestMapping("updateNotice.html")
    public String updateNotice(Notice notice, HttpServletRequest request) {
        noticeService.updateNotice(notice);
        request.setAttribute("bs", "修改公告信息成功");
        return "redirect:to_showNoticeList.html";
    }

    @RequestMapping("/deleteNotice.html")
    public String deleteNotice(int id){
        noticeService.deleteNotice(id);
        return "/WEB-INF/pages/admin/NoticeList";
    }
    @RequestMapping("toShowContent")
    public String getContent(Integer id,HttpServletRequest request){
        Content content=noticeService.getContentByNid(id);
        request.setAttribute("content1",content);
        Integer role = (Integer)request.getSession().getAttribute("USER_TYPE");
        if(role ==1){
            return "/WEB-INF/pages/admin/showContent";
        }if(role==2){
            return "/WEB-INF/pages/teacher/showContent";
        }else{
            return "/WEB-INF/pages/student/showContent";
        }

    }


}
